import { createSlice, PayloadAction} from '@reduxjs/toolkit';

import { StatePagingType } from 'storage/redux';
import RequestStatus from 'storage/redux/RequestStatus';
import {builderPagingHandler} from "storage/redux/reducer";
import {fetchAddReport, fetchDeleteReport, fetchUpdateReport, fetchReportPageData} 
    from "storage/redux/async-action/reportAsyncAction";
import UserStorage from "storage/local/UserStorage";

import {Report} from "type/entity/ReportUser";

/**
 * 报销单表状态管理类型
 */
export type ReportState = StatePagingType<Report>

/** 状态初始化数据 */
const initialState: Report = {
    data: [],
    loading: false,
    requestStatus: RequestStatus.INIT,
    currentPage: 1,
    pageSize: UserStorage.getInstance().getAdminPageConfigFromLocalStore().reportPageSize,
    total: 0,
    totalPage: 10
};

export const reportSlice = createSlice({
    name: 'report',
    initialState,
    reducers: {
    },
    extraReducers: (builder) => {
        builderPagingHandler(builder, fetchReportPageData);
        /** 添加报销单表信息 */
        builder.addCase(fetchAddReport.fulfilled, (state, action) => {
            state.requestStatus = RequestStatus.REFRESH;
        });
        /** 修改报销单表信息 */
        builder.addCase(fetchUpdateReport.fulfilled, (state, action) => {
            state.requestStatus = RequestStatus.REFRESH;
        });
        /** 报销单表用户信息 */
        builder.addCase(fetchDeleteReport.fulfilled, (state, action) => {
            state.requestStatus = RequestStatus.REFRESH;
        });
    }
});

export const {} = reportSlice.actions;
export default reportSlice.reducer;

